import math,random,time,ship
from player import Player
from ship import Ship
from plant import Plant
from comm.loadsave import loadData,saveData
from comm.ui import ItemList,StatePanel
from comm.style import *

# 载入游戏数据
file = "data/gameData.json"
data = loadData(file)

playerData = data['player']
player = Player(playerData)
print(player)
myship = Ship(data['ships'][player.activeShip])
itm = ItemList(myship)
stp = StatePanel(myship)
price = {"煤":4,"铁":6,"铜":8,"石头":1,"金":16,"钻石":20,"锡":8}


# 初始化
def initGame():
    s_clean()

# 刷新面板
def refresh():
    print(s_save)
    print(itm.refresh()+stp.refresh())
    print(s_resume)

initGame()

while True:

    refresh()
    
    a = input("售卖(s) 挖矿(d)>>>")
    # 挖矿
    if a == 'd':
        b = ''
        
        while (b != 'q') and (b != 'exit'):  
            p = Plant()
            while p.cap > 0:
                myship.dig(p)
                refresh()
            b = input(s_lblack+"挖掘(d)>" + s_reset +'按回车继续>>')
    # 售卖货物
    elif a == 's':
        b = ''
        while b != 'q' and b != 'exit':
            b = input(s_lblack+"售卖(s)>" + s_reset + "编号 数量>>")
            if b == 'q' or b == 'exit':
                continue
            else:
                n1 , n2= map(int,b.split(' '))
                player.coin += myship.sell(price,n1,n2)
                print(f'\n玩家星币数：{player.coin}')
                refresh()

            
        
    elif a == 'exit':
        # 收集需要保存的数据
        data ['gameData']['ship'] = myship.getData()
        # 保存
        saveData(file,data['gameData'])
        break
            
